//
//  ViewController.m
//  VFL可视化格式语言
//
//  Created by huanlv on 2017/1/11.
//  Copyright © 2017年 huanlv. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
}
- (void)setUpUI {
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:blueView];
    [self.view addSubview:redView];
    
    // 禁用autoresizing，也可以直接设置self.view.translatesAutoresizingMaskIntoConstraints = NO;
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 添加水平方向的约束
    
    /**
     使用AutoLayout中的VFL来实现布局

     @param format VFL语句
     @param opts 枚举参数，默认写0。如果是多个值，用或（“|”）隔开
     @param metrics 定义VFL中（）指定的常数映射关系
     @param views 是一个NSDictionary，把VFL语句中的控件与创建的控件对象绑定(即定义VFL中的控件名称和实际名称的映射关系)，如下面的@{@"blueView":blueView},也可以这样写：NSDictionaryOfVariableBindings(blueView)，如果是绑定多个对象：NSDictionaryOfVariableBindings(obj1,obj2)
     @return 约束数组
     */
    //+ (NSArray<__kindof NSLayoutConstraint *> *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(nullable NSDictionary<NSString *,id> *)metrics views:(NSDictionary<NSString *, id> *)views;
    NSArray *hCons = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView]-20-|"
                                                             options:0
                                                             metrics:nil
                                                             views:NSDictionaryOfVariableBindings(blueView)];
    [self.view addConstraints:hCons];
    
    // 添加垂直方向的约束
    NSDictionary *metricsDict = @{@"vSpacingTop": @(20),
                                   @"vSpacingBottom": @(20)};
    NSArray *vCons = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(vSpacingTop)-[blueView(100)]-(vSpacingBottom)-[redView(==blueView)]"
                                                             options:NSLayoutFormatAlignAllRight
                                                             metrics:metricsDict
                                                             views:NSDictionaryOfVariableBindings(blueView,redView)];
    [self.view addConstraints:vCons];
    
    // 添加redView的宽度约束
    NSLayoutConstraint *redW = [NSLayoutConstraint constraintWithItem:redView
                                                              attribute:NSLayoutAttributeWidth
                                                              relatedBy:NSLayoutRelationEqual
                                                               toItem:blueView
                                                            attribute:NSLayoutAttributeWidth
                                                           multiplier:.5
                                                             constant:0];
    [self.view addConstraint:redW];
}

@end
